﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Web;

namespace RssReader.Core.BL
{
	[Serializable]
	public class User
	{
		#region Private Properties
		private string _email;
		private string _password;
		private IList<UriPath> _uriList;

		private bool _isNew;
		private bool _hasChanged;
		#endregion

		#region Public Properties
		[XmlAttribute]
		public string Email {
			get
			{
				return _email;
			}
			set
			{
				if (String.IsNullOrEmpty(_email))
				{
					_email = value;
				}
				else
				{
					UpdateEmail(value);
				}
			}
		}
		[XmlAttribute]
		public string Password {
			get
			{
				return _password;
			}
			set
			{
                if (value != _password && !Validation.Instance.IsEmpty(value))
                {
                    _password = value;
                    _hasChanged = true;
                }
			}
		}
		[XmlArray]
		public UriPath[] UriList
		{
			get
			{
				return _uriList.ToArray();
			}
			set
			{
				_uriList = value.ToList();
			}
		}

		[XmlIgnore]
		public bool HasChanged
		{
			get
			{
				return _hasChanged;
			}
            
		}
		#endregion

		#region Constructors
		public User()
		{
			_uriList = new List<UriPath>();
		}

		public User(string email) : this()
		{
			if (Global.Utility.ExistsEmail(email))
			{
				throw new ArgumentException("this email is exist");
			}
			_email = email;
			_isNew = true;
		}

		#endregion

		#region Private Methods
		private bool ExistUri(string path)
		{
			return _uriList.Any(u => u.Path.ToLower() == path.ToLower());
		}
		#endregion

		#region Public Methods
		public static User Load(string email)
		{
			string path = Global.Utility.GetDataPath(email);
			try
			{
				return Global.SerializeEngine<User>.Load(path);
			}
			catch (Exception ex)
			{
				return null;
			}
		}

		public void AddUri(string path, string name)
		{
			if (!ExistUri(path))
			{
				UriPath up = new UriPath();
				up.Name = name;
				up.Path = path;
				_uriList.Add(up);
				_hasChanged = true;
			}
		}

		public void UpdateUri(string oldPath, string newPath, string newName)
		{
			if (_uriList.Any(u => u.Path.ToLower() == oldPath.ToLower()))
			{
				UriPath up = _uriList.Single(u => u.Path.ToLower() == oldPath.ToLower());
				up.Name = newName;
				up.Path = newPath;
				_hasChanged = true;
			}
		}

		public void DeleteUri(string path)
		{
			if (_uriList.Any(u => u.Path.ToLower() == path.ToLower()))
			{
				UriPath up = _uriList.Single(u => u.Path.ToLower() == path.ToLower());
				_uriList.Remove(up);
				_hasChanged = true;
			}
		}

		public void UpdateEmail(string newEmail)
		{
			if (newEmail != _email)
			{
				if (Global.Utility.ExistsEmail(newEmail))
				{
					throw new ArgumentException("this email has been used");
				}
				else
				{
					string path = Global.Utility.GetDataPath(_email);
					File.Delete(path);
					_email = newEmail;
				}
			}
		}
        
           
        

		public void Save()
		{
			string path = Global.Utility.GetDataPath(_email);
			Global.SerializeEngine<User>.Save(path, this);
		}
		#endregion
	}
}
